package com.ghovos.usermangerpgdemo.contorller.api;

import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.digest.Digester;
import com.ghovos.usermangerpgdemo.common.constant.RedisConst;
import com.ghovos.usermangerpgdemo.common.result.GlobalResult;
import com.ghovos.usermangerpgdemo.entity.UserEntity;
import com.ghovos.usermangerpgdemo.service.UserService;
import com.ghovos.usermangerpgdemo.vo.rep.UserEntityRep;
import com.ghovos.usermangerpgdemo.vo.req.UserRoleListReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author EdgarLiu(刘凌峰) <br/>
 * date: 2022/3/17 <br/>
 * time: 下午2:49 <br/>
 * email: EdgarLlf@foxmail.com
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user")
@Api(tags = "用户管理api")
public class UserController {
    private final UserService userService;
    private final StringRedisTemplate stringRedisTemplate;

    @ApiOperation("用户登陆")
    @PostMapping("/login")
    public GlobalResult<Object> login(@RequestBody UserEntity userEntity) {
        UserEntityRep login = userService.login(userEntity);
        if (login != null) {
            return GlobalResult.ok(login);
        }
        return GlobalResult.fail("用户名账户密码失败");
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public GlobalResult<Object> register(@RequestBody UserEntity userEntity) {
        String md5Password = DigestUtil.md5Hex(userEntity.getPassword());
        userEntity.setPassword(md5Password);
        try {
            if (userService.save(userEntity)) {
                return GlobalResult.ok(userEntity);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return GlobalResult.fail("添加失败");
    }

    @ApiOperation("用户退出")
    @PostMapping("/logout")
    public GlobalResult<Object> logout(@RequestHeader(value = "Authorization") String token) {
        userService.logout(token);
        return GlobalResult.ok();
    }

    @ApiOperation(("列出全部用户"))
    @GetMapping("/all")
    public GlobalResult<Collection<UserEntity>> all() {
        List<UserEntity> list = userService.list();
        return GlobalResult.ok(list);
    }

    @ApiOperation("根据id查询一个用户")
    @GetMapping("/find/{id}")
    public GlobalResult<Object> findById(@PathVariable("id") Long id) {
        return GlobalResult.ok(userService.getById(id));
    }

    @ApiOperation("根据id查询一个用户全部信息")
    @GetMapping("/find/ext/{id}")
    public GlobalResult<Object> findExtById(@PathVariable("id") Long id) {
        return GlobalResult.ok(userService.getUserExt(id));
    }

    @ApiOperation("添加一个用户")
    @PostMapping("/add")
    public GlobalResult<Object> addOne(@ApiParam(name = "用户实体") @RequestBody UserEntity userEntity) {
        userEntity.setPassword(
                new Digester(DigestAlgorithm.MD5).digestHex(userEntity.getPassword())
        );
        if (userService.save(userEntity)) {
            return GlobalResult.ok(userEntity);
        }
        return GlobalResult.fail("添加失败");
    }

    @ApiOperation("根据id更新一个用户")
    @PostMapping("/update")
    public GlobalResult<Object> updateOne(@ApiParam(name = "用户实体") @RequestBody UserEntity userEntity) {
        if (userService.updateById(userEntity)) {
            return GlobalResult.ok(userEntity);
        }
        return GlobalResult.fail("更新失败");
    }


    @ApiOperation("根据id删除用户")
    @GetMapping("/delete/{id}")
    public GlobalResult<Object> deleteById(@PathVariable("id") Long id) {
        if (userService.deleteUserWithRoles(id)) {
            return GlobalResult.ok();
        }
        return GlobalResult.fail("删除失败");
    }

    @ApiOperation("列出用户的权限和部门")
    @GetMapping("/all/ext")
    public GlobalResult<List<UserEntity>> allUserExt() {
        return GlobalResult.ok(userService.getUserExtList());
    }


    @ApiOperation("批量添加角色")
    @PostMapping("/add/roles")
    public GlobalResult<Object> addRolesByIds(
            @RequestBody UserRoleListReq userRoleListReq) {
        return GlobalResult.ok(
                userService.addRolesList(
                        userRoleListReq.getUserId(),
                        userRoleListReq.getRoleIdList()
                ));
    }

    @ApiOperation("批量删除角色")
    @PostMapping("/delete/roles")
    public GlobalResult<Object> deleteRolesByIds(
            @RequestBody UserRoleListReq userRoleListReq) {
        return GlobalResult.ok(userService.deleteRolesList(
                userRoleListReq.getUserId(),
                userRoleListReq.getRoleIdList()
        ));
    }


}
